<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="景点" prop="areaId">
        <el-select
          v-model="queryParams.areaId"
          placeholder="请选择景点"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in listTouristAreaOptions"
            :key="dict.addrCode"
            :label="dict.addrName"
            :value="dict.addrCode"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="纬度" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择纬度" clearable size="small">
          <el-option
            v-for="dict in touristTimeTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="时间" prop="touristDay">
        <el-date-picker
          v-model="queryParams.beginTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <!--      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:touristNumbers:export']"
          >导出统计</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExportStatistical"
          v-hasPermi="['app:touristInsight:export']"
          >导出报表</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleQuery(4)"
          >查询当前时间人数</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleQuery(1)"
          >查询所选时间当天总人数</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleQuery(2)"
          >查询所选时间当前小时总人数</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleQuery(3)"
          >查询所选时间人数</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleQuery(5)"
          >查询所选日期历史实时人数</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleQuery(6)"
          >查询本月到天人数</el-button
        >
      </el-col>
      <!--      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>-->
    </el-row>

    <el-table
      v-loading="loading"
      :data="touristNumbersList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="区域" align="center" prop="areaName" />
      <el-table-column
        label="纬度"
        align="center"
        prop="type"
        :formatter="touristTimeTypeFormat"
      />
      <el-table-column
        label="日期"
        align="center"
        prop="touristDay"
        width="180"
      />
      <el-table-column label="人数" align="center" prop="quantity" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客流量对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="区域" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入区域" />
        </el-form-item>
        <el-form-item label="日期" prop="touristDay">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.touristDay"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总人数" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入总人数" />
        </el-form-item>
        <el-form-item label="类型，1 整天总数，2按小时，3按15分钟">
          <el-select
            v-model="form.type"
            placeholder="请选择类型，1 整天总数，2按小时，3按15分钟"
          >
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTouristNumbers,
  getTouristNumbers,
  delTouristNumbers,
  addTouristNumbers,
  updateTouristNumbers,
} from '@/api/app/touristNumbers'
import { listTouristArea } from '@/api/app/touristAddrArea'

export default {
  name: 'TouristNumbers',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dateRange: [],
      // 客流量表格数据
      touristNumbersList: [],
      touristTimeTypeOptions: [],
      listTouristAreaOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        areaId: null,
        touristDay: null,
        quantity: null,
        type: 1,
        orderByColumn: 'tourist_day',
        isAsc: 'desc',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.handleQuery(4)
    this.getDicts('TouristTimeType').then((response) => {
      this.touristTimeTypeOptions = response.data
    })
    listTouristArea({})
      .then()
      .then((response) => {
        this.listTouristAreaOptions = response
      })
  },
  methods: {
    /** 查询客流量列表 */
    getList() {
      this.loading = true
      listTouristNumbers(this.queryParams).then((response) => {
        this.touristNumbersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    touristTimeTypeFormat(row, column) {
      return this.selectDictLabel(this.touristTimeTypeOptions, row.type)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaId: null,
        touristDay: null,
        quantity: null,
        type: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery(type) {
      this.queryParams.pageNum = 1
      this.queryParams.type = type
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery(4)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加客流量'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTouristNumbers(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改客流量'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTouristNumbers(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addTouristNumbers(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除客流量编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delTouristNumbers(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    },
    /** 导出按钮操作 */
    handleExport() {
      // if(this.queryParams.beginTime==null || this.queryParams.beginTime==''){
      //   this.msgError("必须选择时间，先查询后再导出");
      //   return;
      // }
      this.download(
        'app/touristNumbers/export',
        {
          ...this.queryParams,
        },
        `app_touristNumbers.xlsx`
      )
    },
    handleExportStatistical() {
      // window.open('http://zizaishinan.yingketec.com/tsMIdeYRoW.php/index/index', '_blank');
      window.open('http://120.27.123.70:9851/', '_blank')
    },
  },
}
</script>
